package utils;
import java.io.*;
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

public class TcpSocketUtil {
    public static int connect(String message)
    {
        String serverAddress = "127.0.0.1"; // 服务器地址，这里用本地环回地址作为示例
        int port = 11009; // 端口号

        try (Socket socket = new Socket(serverAddress, port)) {
            // 创建输出流，用于向服务器发送数据
            try (OutputStream outputStream = socket.getOutputStream();
                 PrintWriter out = new PrintWriter(outputStream, true)) {

                // 发送消息给服务器
                // String messageToSend = "Hello, Server!";
                System.out.println("Sending: " + message);
                out.println(message);

                // 创建输入流，用于接收来自服务器的数据
                // try (InputStream inputStream = socket.getInputStream();
                //      BufferedReader in = new BufferedReader(new InputStreamReader(inputStream))) {

                //     // 读取服务器的响应
                //     String response;
                //     while ((response = in.readLine()) != null) {
                //         System.out.println("Received: " + response);
                //     }
                // }
                
                return 0;
            } catch (IOException e) {
                System.err.println("Error: " + e.getMessage());
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + serverAddress);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " + serverAddress);
        }

        return -1;
    }
}
